import { Component } from '@angular/core';
import { NavParams, ViewController } from 'ionic-angular';
import { TaskDetailService } from './detail.service';
import { Config } from '../../../providers/config';
import { Storage } from '@ionic/storage';
import _ from 'lodash';


@Component({
  selector: 'page-task-bt0107',
  templateUrl: './bt0107.html',
  providers: [TaskDetailService]
})
export class BT0107Page {
	moa_page_name: any = "page-task-bt0107";
  moa_page_explain: any = "流程明细列表";

  // 明细列表数据
  item;
  // 模板id
  bomid;
  // 格式化后的明细列表数据
  formatedItem: any = [];
  // 明细列名称
  colNames: any;


  constructor(
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public storage: Storage
  ) {
    this.item = this.navParams.get("item");
    this.bomid = this.navParams.get("bomid");
    this.dataFomate();

  }

  complete() {
    this.dismiss();
  }

  dismiss(data?: any) {
    // using the injected ViewController this page
    // can "dismiss" itself and pass back data
    this.viewCtrl.dismiss(data);
  }

  checkIsSelected(item) {
    item.isShow = !item.isShow;
  }

  /**
   * 格式化数据
   * [
   * {
   *  "name" :; // 明细1
   *  "isShow":;
   *  "list" : [{"key": "标题","value":"值",{"key":,"value"}]
   * },{
   *  "name" :; // 明细2
   *  "isShow":;
   *  "list" : [{"key":,"value"},{"key":,"value"}]
   * }
   * ]
   */
  dataFomate() {
    let that = this;
    this.colNames = this.item.businessDetailDto.colNames;

    if (this.item.businessDetailDto && this.item.businessDetailDto.rowDatas) {
      _.each(this.item.businessDetailDto.rowDatas, function(rowData, index) {
        let temp: any = {
          "name" : "",
          "isShow": false,
          "list": []
        };

        temp.name = "明细" + (index + 1);
        temp.isShow = false;

        _.forEach(rowData.item,function(data, index){
          let temp01: any = {
            "key" : "", // 标题
            "value" : "" // 值
          };
          temp01.key = that.colNames[index];
          temp01.value = data;
          temp.list.push(temp01);
        })
        that.formatedItem.push(temp);

        // 特殊处理
        that.specialHandler(that.bomid);
      })
    }
  }

  /**
   * [specialHandler 特殊处理]
   */
  specialHandler(bomid){
    switch (bomid) {
      case "T_CM_NJDSQ": // 接待申请
        this.T_CM_NJDSQ_handler(this.formatedItem);
        break;
    }
  }

  /**
   * [T_CM_NJDSQ_handler 接待申请特殊处理]
   */
  T_CM_NJDSQ_handler(items){
    var that = this;
    _.forEach(items,function(item){
      let qcptry = _.find(item.list,{"key" : "全程陪同人员"});
      let qcptryList = (qcptry && qcptry != undefined && qcptry != null) ? _.split(qcptry["value"], ",") : [];

      let qtcyry = _.find(item.list,{"key" : "其他参与人员"});
      let qtcyryList = (qtcyry && qtcyry != undefined && qtcyry != null) ? _.split(qtcyry["value"], ",") : [];

      let zxr = _.find(item.list,{"key" : "撰写人"});
      let zxrList =  (zxr && zxr != undefined && zxr != null) ? _.split(zxr["value"], ",") : [];

      let shr = _.find(item.list,{"key" : "审核人"});
      let shrList = (shr && shr != undefined && shr != null) ? _.split(shr["value"], ",") : [];

      that.storage.get('contact-user-list').then(function(userlist){ // 从本地通讯录中，获得人员信息

        // 将陪同人员的EMPID 转换成名字
        let temp = [];
        _.forEach(qcptryList,function(empid){ // 遍历全程陪同人员 EMPID 转换成名字
          let emname = _.find(userlist,{"empid" : empid})["emname"];
          temp.push(emname? emname : empid);
        })

        if(qcptry && qcptry != undefined && qcptry != null) {
          qcptry["value"] = _.toString(temp);
        }


        // 其他参与人员的EMPID 转换成名字
        let temp01 = [];
        _.forEach(qtcyryList,function(empid){ // 遍历全程陪同人员 EMPID 转换成名字
          let emname = _.find(userlist,{"empid" : empid})["emname"];
          temp01.push(emname? emname : empid);
        })

        if(qtcyry && qtcyry != undefined && qtcyry != null) {
          qtcyry["value"] = _.toString(temp01);
        }

        // 将撰写人的EMPID 转换成名字
         let temp02 = [];
        _.forEach(zxrList,function(empid){ // 遍历全程陪同人员 EMPID 转换成名字
          let emname = _.find(userlist,{"empid" : empid})["emname"];
          temp02.push(emname? emname : empid);
        })

        if(zxr && zxr != undefined && zxr != null) {
          zxr["value"] = _.toString(temp02);
        }

        // 将审核人的EMPID 转换成名字
         let temp03 = [];
        _.forEach(shrList,function(empid){ // 遍历全程陪同人员 EMPID 转换成名字
          let emname = _.find(userlist,{"empid" : empid})["emname"];
          temp03.push(emname? emname : empid);
        })

        if(shr && shr != undefined && shr != null) {
          shr["value"] = _.toString(temp03);
        }

      })

    })

  }

}
